<template>
  <transition-group name="list" tag="ul" class="todo-list">
    <!-- 移除未使用的 index -->
    <li v-for="todo in todos" :key="todo.id">
      <input type="checkbox" v-model="todo.done" @change="saveTodos" />
      <span :class="{ done: todo.done }">{{ todo.text }}</span>
      <button class="delete-btn" @click="deleteTodo(todo.id)">删除</button>
    </li>
  </transition-group>
</template>

<script setup lang="ts">
import { defineProps, defineEmits } from 'vue'

const props = defineProps<{
  todos: { id: number; text: string; done: boolean }[]
}>()

console.log(props.todos)

const emits = defineEmits(['delete', 'save'])

function deleteTodo(id: number) {
  emits('delete', id)
}

function saveTodos() {
  emits('save')
}
</script>

<style scoped>
.todo-list {
  list-style: none;
  padding: 0;
}

.todo-list li {
  display: flex;
  align-items: center;
  background: #fafafa;
  margin-bottom: 10px;
  padding: 10px 15px;
  border-radius: 10px;
  transition: all 0.3s ease;
}

.todo-list li:hover {
  transform: translateY(-3px);
  box-shadow: 0 4px 8px rgba(0,0,0,0.1);
}

.todo-list input[type="checkbox"] {
  margin-right: 10px;
  cursor: pointer;
}

.todo-list span {
  flex: 1;
  text-align: left;
  word-break: break-word;
}

.todo-list span.done {
  text-decoration: line-through;
  color: #aaa;
}

.delete-btn {
  background-color: #ff4d4f;
  color: white;
  border: none;
  padding: 6px 10px;
  border-radius: 8px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.delete-btn:hover {
  background-color: #d9363e;
}
</style>